Skip to content

Conversation

@Alaa2019-ml
Copy link

No description provided.

Copy link

@RHSebregts RHSebregts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Alaa,

Great work here! I left some remarks, but based on what I'm seeing here it seems like you have a good grasp on the basics of API's. Your test suite is also very good! There are some small changes you could make, to polish it even more.

Happy coding!

app.post("/weather", async (req, res) => {
const { cityName } = req.body;
if (!cityName || cityName.trim() === "") {
return res.status(400).json({ msg: "City name is required." });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice guard clause!

try {
const response = await fetch(url);
if (!response.ok) {
return res.status(400).json({ weatherText: "City is not found!" });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're already in a try-catch we could also throw here.

});

describe("Test POST /weather", () => {
test("Return city name is required.", async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how this test is using the AAA principle. This makes it very readable, great!

const res = await request.post("/weather").send({ cityName: city });

expect(res.statusCode).toBe(200);
expect(res.body.main.temp).toBe(286.78);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also test for the city name here, to make sure we get all the correct data.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good test suite, well done! One small thing you could change is the order and grouping of the tests. You can put multiple tests into one describe block, which makes it easier to see which tests belong together, and cleans up your test logs a bit. It's also good to pick an order for tests, group them by end point and then either start with the happy path test and then error cases, or the other way around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants